<?php
    ////////////////////////////////////////////////////////////////////////
    //// CORE 
    //// MODEL CLASS
    //// INSERT UPDATE DELETE
    ////////////////////////////////////////////////////////////////////////    
    
    ( defined('BASEPATH') ) OR  exit('No direct access script allow !');
    
    class MY_Model extends CI_Model
    {
        var $tbl_name;                

        public function __construct()
        {
            parent::__construct();
        }
        
        function total_rows($options = false)
        {
            $this->db->select('*');
            $this->db->from($this->tbl_name);
            
            if( $options )
            {
                foreach ($options as $key => $value) 
                {
                    $this->db->where($key,$value);
                }
            }

            // if active is set
            $this->db->where('active','1');
            $query = $this->db->get();
                   
            return $query->num_rows();
        }

        // Get all data
        function getAllData($options = false, $order = false, $start = FALSE, $limit = FALSE)
        {                   
            $this->db->select('*');
            $this->db->from($this->tbl_name);
            
            if( $options )
            {
                foreach ($options as $key => $value) 
                {
                    $this->db->where($key,$value);
                }
            }

            if ( $order ) 
            {
                foreach ($order as $key => $value) 
                {
                    $this->db->order_by($key,$value);
                }
            }
            
            // Pagination
            if( $limit )
            {
                $this->db->limit($limit, $start);
            }

            // if active is set
            $this->db->where('active','1');
            $query = $this->db->get();
                   
            return $query->result_array();
        }

        function getCurrentData($field = false,$data = false)
        {
            $field_value = $this->input->post($field);
            if( isset($field_value) && !empty($field_value) )
            {
                return $field_value;
            }
            else
            {
                return $data[$field];
            }
        }

        // Get data
        function getData($id = false, $options = false)
        {                   
            $this->db->select('*');
            $this->db->from($this->tbl_name);
            if( $id )
            {
                $this->db->where('id',$id);
            }            
            else if( $options )
            {
                foreach ($options as $key => $value) {
                    $this->db->where($key,$value);
                }
            }
            else
            {
                // no condition 
                return '';
            }
            $this->db->where('active','1');

            $query = $this->db->get();
                   
            return $query->row_array();
        }
                
        // Insert
        function insert($data = false)
        {
            return $this->db->insert($this->tbl_name,$data);
        }

        // Update 
        function update($data = false,$id = false,$options = false)
        {
            if( $options )
            {
                $this->db->where($options['key'],$options['value']);
            }
            else
            {
                $this->db->where('id',$id);                 
            }            

            return $this->db->update($this->tbl_name,$data);
        }

        // Delete 
        function delete($id = false)
        {
            // $this->db->where('id',$id);
            // $this->db->delete($this->tbl_name);

            // As we not delete it, we just de-active it
            $data['active'] = 0;
            return $this->update($data,$id);
        }
    }
